package com.mdd.admin.service.impl;

import com.alibaba.fastjson2.JSON;
import com.mdd.admin.service.ISettingSmsModelService;
import com.mdd.common.util.*;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 通知设置服务实现类
 */
@Service
public class SettingSmsModelServiceImpl implements ISettingSmsModelService {

    /*
    * 短信模板
    * */
    @Override
    public List<Map<String, String>> list() {
        String config = ConfigUtils.get("sms", "model");
        List<Map<String, String>> maps = ListUtils.stringToListAsMapStr(config);
        return maps;
    }

    @Override
    public Map<String, String> detail(String key) {
        String config = ConfigUtils.get("sms", "model");
        List<Map<String, String>> maps = ListUtils.stringToListAsMapStr(config);
        return maps.stream().filter(map -> map.get("key").equals(key)).findFirst().orElse(null);
    }

    @Override
    public void save(Map<String, String> params) {
        String config = ConfigUtils.get("sms", "model");
        List<Map<String, String>> maps = ListUtils.stringToListAsMapStr(config);
        for (Map<String, String> map : maps) {
            if (map.get("key").equals(params.get("key"))){
                //如果存在key则要替换这个map数据
                map.put("status",params.get("status"));
                map.put("modelId",params.get("modelId"));
            }
        }
        //处理完数据后再转成json保存到数据库
        ConfigUtils.set("sms", "model", JSON.toJSONString(maps));
    }
}
